<?php

namespace IMooc;

class Model
{
    protected array $observers = [];

    function __construct()
    {
        $name = strtolower(str_replace('App\Model\\', '', get_class($this)));
        // 观察者模式
        if (!empty(Application::getInstance()->config['model'][$name]['observer'])) {
            $observers = Application::getInstance()->config['model'][$name]['observer'];
            foreach ($observers as $class) {
                $this->observers[] = new $class;
            }
        }
    }

    function notify($event): void
    {
        foreach ($this->observers as $observer) {
            // 触发事件【更新逻辑】
            $observer->update($event);
        }
    }
}